package com.atguigu.java2;

import com.atguigu.java1.Person;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 运行时类方法结构
 */
public class MethodTest {

    @Test
    public void test1() {
        //获取当前类以及当前类父类所有为public的方法
        Class clazz = Person.class;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println();
        //获取当前类所有的方法,无论什么修饰符,不包含父类
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

    /**
     * @xxx 权限修饰符, 返回值类型, 方法名(参数类型1 形参名1.....)throws xxx{}
     */
    @Test
    public void test2() {
        //获取方法声明注解
        Class clazz = Person.class;
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            Annotation[] annotations = declaredMethod.getAnnotations();
            for (Annotation annotation : annotations) {
//                System.out.println(annotation);
            }
            //权限修饰符
//            System.out.println(Modifier.toString(declaredMethod.getModifiers()));
            //返回值类型
            String name = declaredMethod.getReturnType().getName();
//            System.out.println(name);
            //方法名
            String name1 = declaredMethod.getName();
            System.out.println(name1);

            //形参列表
            Class[] parameterTypes = declaredMethod.getParameterTypes();
            if (!(parameterTypes.length == 0)) {
                for (int i = 0; i < parameterTypes.length; i++) {
//                    System.out.println(parameterTypes[i].getName() + "args_" + i + ",");
                }

            }

            //抛出的异常

            Class<?>[] exceptionTypes = declaredMethod.getExceptionTypes();
            for (Class<?> exceptionType : exceptionTypes) {
                exceptionType.getName();
            }
        }
    }


}

